//
//  MineViewController.m
//  PropertyMananger
//
//  Created by bh on 16/12/21.
//  Copyright © 2016年 BH. All rights reserved.
//

#import "MineViewController.h"
#import "MineCell.h"
#import "TableHeaderView.h"
#import "LoginViewController.h"
#import "MyWorkController.h"
#import "InspectionController.h"
#import "ModifyController.h"
#import "PersonalController.h"
#import "AboutUsController.h"

static NSString * headerID = @"headerID";
@interface MineViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate>
/**个人列表**/
@property (nonatomic,strong) UITableView * mTableView;
/**图片数据源**/
@property (nonatomic,strong) NSArray * mImgArr;
/**标题数据源**/
@property (nonatomic,strong) NSArray * mTitleArr;
@property (nonatomic,strong) TableHeaderView * headerView;

@property (nonatomic,copy) NSString * selectedCommunityName;


@property (nonatomic,copy) NSString * myComplaintcount;//我的投诉单数量
@property (nonatomic,copy) NSString * myRepaircount;//我的报修单单数量

@end

@implementation MineViewController
- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    self.tabBarController.hidesBottomBarWhenPushed = NO;
    
    self.navigationController.navigationBar.hidden = YES;
    
    [self postWorkNumData];

}

- (void)hiddenTabbar{

    self.tabBarController.hidesBottomBarWhenPushed = YES;

}

- (void)viewDidLoad {
    [super viewDidLoad];
   
    [self createTableView];
    
    [self initData];
    
    // 设置导航控制器的代理为self
    self.navigationController.delegate = self;
    
    [self postWorkNumData];
    
     [MBProgressHUD hideHUDForView:self.view];
    
    
}


// 将要显示控制器
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    // 判断要显示的控制器是否是自己
    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
    
    [self.navigationController setNavigationBarHidden:isShowHomePage animated:YES];
}
#pragma mark - 初始化tableView
- (void)createTableView
{

    self.mTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -20, BHscreenW, BHscreenH - 64) style:UITableViewStylePlain];
    self.mTableView.delegate = self;
    self.mTableView.dataSource = self;
    self.mTableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];
    [self.view addSubview:self.mTableView];
    self.mTableView.backgroundColor = BHGrayColor;
    
    
    //表头
    self.headerView = [[TableHeaderView alloc]initWithFrame:CGRectMake(0, 0, BHscreenW, 200)];
     self.mTableView.tableHeaderView = self.headerView;
    
    [self loginVC];
  
}
- (void)loginVC
{

  [self.headerView.exsitBtn addTarget:self action:@selector(exsitBtnClick:) forControlEvents:UIControlEventTouchUpInside];

}
- (void)exsitBtnClick:(UIButton *)btn
{
    DLog(@"退出");
   
    //极光推送设置别名为空
    [JPUSHService setTags:nil aliasInbackground:nil];
    
    CKAlertViewController *alertVC = [CKAlertViewController alertControllerWithTitle:@"退出" message:@"确认退出当前账号" ];
    
    CKAlertAction *cancel = [CKAlertAction actionWithTitle:@"取消" handler:^(CKAlertAction *action) {
        NSLog(@"点击了 %@ 按钮",action.title);
    }];
    
    CKAlertAction *sure = [CKAlertAction actionWithTitle:@"确定" handler:^(CKAlertAction *action) {
        NSLog(@"点击了 %@ 按钮",action.title);
        
        [[NSNotificationCenter defaultCenter] postNotificationName:exsitLoginNoticeCenter object:nil];
    }];
    
    [alertVC addAction:cancel];
    [alertVC addAction:sure];
    
    [self presentViewController:alertVC animated:NO completion:nil];
   
    
}
#pragma mark - 获取数据
- (void)postWorkNumData{
    
//    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
//    hud.label.text = @"努力加载数据中...";
//    // 隐藏时从父控件中移除
//    hud.removeFromSuperViewOnHide = YES;
//    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
//    hud.userInteractionEnabled = YES;
    
    NSDictionary * parms = nil;
    parms = @{
              @"WorkerId":userModel.WorkerID,
              @"CommunityID":userModel.CommunityID,
              @"json":@"\{\"jsonEntity\":\{},\"action\":\"incompletetask\",\"version\":\"V1.0\"}"
              };
    
    [WorkOrderRequest postWorkNumSuccess:^(id data) {
        
     //   [MBProgressHUD hideHUDForView:self.view];
        
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        NSString * str = [NSString stringWithFormat:@"%@",dict[@"Data"]];
        NSData * data2 = [str dataUsingEncoding:NSUTF8StringEncoding];
        NSArray * array = [NSJSONSerialization JSONObjectWithData:data2 options:NSJSONReadingMutableContainers error:nil];
        DLog(@"工单数量的数据 == %@",array);
        for (NSDictionary * dict in array) {
            
            self.myRepaircount = [NSString stringWithFormat:@"%@",dict[@"MyRepaircount"]];
            self.myComplaintcount = [NSString stringWithFormat:@"%@",dict[@"MyComplaintcount"]];
            DLog(@"报修单数量 == %@ 我的投诉单 == %@",self.myRepaircount,self.myComplaintcount);
            
        }
        
        [self.mTableView reloadData];
        
    } Fail:^(NSString *errorDes) {
       // [MBProgressHUD hideHUD];
    } param:parms];
    
}

#pragma makr - 数据源
- (void)initData
{
    //icon
    self.mImgArr = [[NSArray alloc]init];
    NSArray * imgArr1 = @[@"ico_my_work_order",@"ico_my_patrol",@"ico_my_personal_information",@"ico_my_modify_password"];
    NSArray * imgArr2 = @[@"ico_my_about_us"];
    self.mImgArr = @[imgArr1,imgArr2];
    //标题
    NSArray * titleArr1 = @[@"我的工单",@"我的巡检",@"个人信息",@"修改密码"];
    NSArray * titleArr2 = @[@"关于我们"];
    self.mTitleArr = @[titleArr1,titleArr2];
    
}
#pragma mark - 代理方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
     NSArray * array = self.mImgArr[section];
    return array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

        
        MineCell * mineCell = [MineCell cellWithTableView:tableView];
        mineCell.iconImg.image = [UIImage imageNamed:self.mImgArr[indexPath.section][indexPath.row]];
        mineCell.titleLabel.text = self.mTitleArr[indexPath.section][indexPath.row];
    
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
             mineCell.messageIcon.image = IMG(@"ico_new");
            
            if ([self.myRepaircount isEqualToString:@"0"] && [self.myComplaintcount isEqualToString:@"0"]) {
                
                mineCell.messageIcon.hidden = YES;
            }else{
            
                mineCell.messageIcon.hidden = NO;
            }
            
        }

    }
    
        return mineCell;

}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        if (indexPath.row == 0) {
            
            MyWorkController * myWorkVC = [[MyWorkController alloc]init];
            
            [self.navigationController pushViewController:myWorkVC animated:YES];
        }else if (indexPath.row == 1){
            
            InspectionController * myInspectionVC = [[InspectionController alloc]init];
            [self.navigationController pushViewController:myInspectionVC animated:YES];
        }else if (indexPath.row == 2){
        
            PersonalController * personVC = [[PersonalController alloc]init];
            [self.navigationController pushViewController:personVC animated:YES];
        
        }else if (indexPath.row == 3){
        
            ModifyController * modifyVC = [[ModifyController alloc]init];
            [self.navigationController pushViewController:modifyVC animated:YES];
        
        }

    }else if (indexPath.section == 1){
        
        if (indexPath.row == 0) {
            
            AboutUsController * aboutUsVC = [[AboutUsController alloc]init];
            [self.navigationController pushViewController:aboutUsVC animated:YES];
            
        }
        
        
    }
    
    DLog(@"点击第%ld行",indexPath.row + 1);
   
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 1) {
        
        return 15;
    }
    return 0.001f;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001f;
}

@end
